import { createRouter, createWebHashHistory } from 'vue-router'
import menuRouter from './menuRouter'
import { useUserStore } from '@/store/modules/user'
// 静态路由
const constantRoute = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    name: '404',
    path: '/404',
    component: () => import('@/pages/404/index.vue'),
    meta: {
      title: '404页面未找到'
    }
  },
  {
    label: '登录',
    name: 'login',
    path: '/login',
    component: () => import('@/pages/login/index.vue'),
    meta: {
      layout: 'login'
    }
  },
  {
    path: '/:patchAll(\\S+)',
    redirect: '/404'
  },
  ...menuRouter
]
const router = createRouter({
  routes: constantRoute,
  history: createWebHashHistory()
})

// 路由拦截
router.beforeEach((to, from, next) => {
  const store = useUserStore()
  store.clearCancel()
  if (to.fullPath === '/login') {
    next()
  } else if (store.isLogin) {
    next()
  } else {
    next('/login')
  }
})

export default router
